package com.ljpz.search.service;

import com.ljpz.search.bean.Item;
import com.ljpz.search.bean.SearchResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by liujunchen on 2016/12/27.
 */
@Service
public class ItemSearchService {
    @Autowired
    private HttpSolrServer  httpSolrServer ;
    public SearchResult    search(String keywords, Integer page, Integer rows) throws SolrServerException {
        SolrQuery  solrQuery= new SolrQuery(); //构造搜索条件
        solrQuery.setQuery("title:"+keywords+" AND status:1")  ; //搜索关键词
        //设置分页start=0 就是从0开始，rows=5  当前返回5条记录，第二页就是变化start  这个值为5就可以了
        solrQuery.setStart((Math.max(page,1)-1)*rows)  ;
        solrQuery.setRows(rows)  ;
        //是否需要高亮
        boolean ishighlighting= !StringUtils.equals("*",keywords)&&StringUtils.isNotEmpty(keywords)  ;
        if (ishighlighting){
            //设置高亮
            solrQuery.setHighlight(true) ;//开启高亮组件
            solrQuery.addHighlightField("title");//高亮字段
            solrQuery.setHighlightSimplePost("</em>");//标记， 高亮关键词前缀
            solrQuery.setHighlightSimplePre("<em>") ;   //前缀
        }
        //执行查询
        QueryResponse queryResponse =  this.httpSolrServer.query(solrQuery);
        List<Item>  items=queryResponse.getBeans(Item.class) ;
        if (ishighlighting){
            //将高亮标题数据写回到数据对象中

            Map<String, Map<String,List<String>>>map = queryResponse.getHighlighting();

            for (Map.Entry<String,Map<String,List<String>>>highlighting:map.entrySet()){
                for (Item item :items){
                    if (!highlighting.getKey().equals(item.getId().toString())){
                        continue;
                    }
                    item.setTitle(StringUtils.join(highlighting.getValue().get("title"),""));
                    break;
                }
            }
        }
        return   new SearchResult(queryResponse.getResults().getNumFound(),items) ;
    }
}
